#include "CodeThread.h"


CodeThread::CodeThread(CodeProcess &codeprocess):code(codeprocess)
{
		stop = false;
		
}


CodeThread::~CodeThread()
{

		Stop();
}


int CodeThread::Start()
{
	if (JThread::IsRunning())
	{
		return -5;
	}
	if (stopmutex.IsInitialized())
	{
		if (stopmutex.Init()<0)
		{
			return -1;
		}
	}
	stop = false;
	if (JThread::Start()<0)
	{
		return	-2;
	}
	return 0;
}
void CodeThread::Stop()
{
	if(!IsRunning())
	{
		return ;
	}
	stopmutex.Lock();
	stop = true;
	stopmutex.Unlock();
	if(JThread::IsRunning())
	{
		JThread::Kill();
	}
	stop = false;
}

void* CodeThread::Thread()
{
	JThread::ThreadStarted();
	while(stop == false)
	{
		code.ProcessCode();
		Sleep(1);
	}
	
	return NULL;
}